﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    //  首页控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IndexController : ControllerBase
    {
        private IHttpContextUser _user;
        private IXrcNavigationServices _navigationServices;
        private IXrcGroupServices _groupServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="user"></param>
        /// <param name="noticeServices"></param>
        public IndexController(IHttpContextUser user, IXrcNavigationServices navigationServices , IXrcGroupServices groupServices)
        {
            _user = user;
            _navigationServices = navigationServices;
            _groupServices = groupServices;
        }


        #region 列表
        /// <summary>
        /// 导航列表  id 传0 导航  1 轮播图
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> NoticeList([FromBody] FMPageByIntId entity)
        {
            var jm = new WebApiCallBack();
            Expression<Func<XrcNavigation, bool>> where = p => p.delete == 0 && p.type == entity.id && p.state == 1;

            int storeId = entity.store > 0 ? entity.store : _user.Store > 0 ? _user.Store : 1;
            where = where.And(p => p.gid == storeId);

            var list = await _navigationServices.QueryPageAsync(where, p => p.creat_time, OrderByType.Desc,
                entity.page, entity.limit);
            jm.status = true;
            jm.data = list;
            return jm;
        }

        #endregion

        /// <summary>
        /// 获取单个内容
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> NoticeInfo([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _navigationServices.QueryByIdAsync(entity.id);
            if (model == null)
            {
                jm.msg = "数据获取失败";
                return jm;
            }
            jm.status = true;
            jm.data = model;
            return jm;
        }


        /// <summary>
        /// 获取店铺信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> StoreInfo([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _groupServices.QueryByIdAsync(entity.id);
            if (model == null)
            {
                jm.msg = "数据获取失败";
                return jm;
            }
            jm.status = true;
            jm.data = model;
            return jm;
        }
    }
}
